﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;

public partial class Signup : System.Web.UI.Page
{
    bool inputOK;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Check_Input()
    {
        if (!string.IsNullOrEmpty(txtName.Text))
        {
            if (!Check_NameRegex(txtName.Text))
            {
                Image1.Visible = false;
                Label4.Visible = false;
            }
            else
            {
                Image1.Visible = Check_Uname(txtName.Text) ? true : false;
                Label4.Visible = Check_Uname(txtName.Text) ? false : true;
            }
        }
        if (!string.IsNullOrEmpty(txtEmail.Text))
        {
            if (!Check_MailRegex(txtEmail.Text))
            {
                Image2.Visible = false;
                Label3.Visible = false;
            }
            else
            {
                Label3.Visible = Check_Email(txtEmail.Text) ? false : true;
                Image2.Visible = Check_Email(txtEmail.Text) ? true : false;
            }
        }
        
        lblUName.Visible = Check_Null(txtName.Text) ? false : true;
        lblRegExUserName.Visible = Check_NameRegex(txtName.Text) ? false : true;
        lblPW1.Visible = Check_Null(txtPass1.Text) ? false : true;
        lblPW2.Visible = Check_Null(txtPass2.Text) ? false : true;
        CompareValidator1.Visible = Check_Equal(txtPass1.Text, txtPass2.Text) ? false : true;
        lblFName.Visible = Check_Null(txtFName.Text) ? false : true;
        
        lblEmail1.Visible = Check_Null(txtEmail.Text) ? false : true;
        lblMailRegex.Visible = Check_MailRegex(txtEmail.Text) ? false : true;
        lblEmail2.Visible = Check_Null(txtEmail2.Text) ? false : true;
        CompareValidator2.Visible = Check_Equal(txtEmail.Text, txtEmail2.Text) ? false : true;
        lblPhone.Visible = Check_Null(txtPhone.Text) ? false : true;
        lblPhoneRegex.Visible = Check_PhoneRegex(txtPhone.Text) ? false : true;
        if (Check_PhoneRegex(txtPhone.Text) && Check_Null(txtPhone.Text) && Check_Equal(txtEmail.Text, txtEmail2.Text)
            && Check_Null(txtEmail2.Text) && Check_MailRegex(txtEmail.Text) && Check_Null(txtEmail.Text) && Check_Email(txtEmail.Text)
            && Check_Null(txtFName.Text) && Check_Equal(txtPass1.Text, txtPass2.Text) && Check_Null(txtPass2.Text) && Check_Null(txtPass1.Text)
            && Check_NameRegex(txtName.Text) && Check_Null(txtName.Text) && Check_Uname(txtName.Text))
            inputOK = true;
        else inputOK = false;
    }
    protected bool Check_Equal(string input1, string input2)
    {
        if (input2 != input1)
            return false;
        else return true;
    }
    protected bool Check_NameRegex(string input)
    {
        Regex myReg = new Regex(@"^(\w*)$");
        if (!myReg.IsMatch(input))
            return false;
        else return true;
    }

    protected bool Check_MailRegex(string input)
    {
        Regex myReg = new Regex(@"^(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)$");
        if (!myReg.IsMatch(input))
            return false;
        else return true;
    }
    protected bool Check_PhoneRegex(string input)
    {
        Regex myRegex = new Regex(@"^(0(\d{9)|\d{10})$");
        if (!myRegex.IsMatch(input))
            return false;
        else return true;
    }
    protected bool Check_Null(string input)
    {
        if (string.IsNullOrEmpty(input))
            return false;
        else
            return true;
    }
    protected bool Check_Uname(string uname)
    {
        string sql1 = string.Format(@"select CustomerID From customers Where CustomerID='{0}'", uname);
        DataTable dt1 = DataBase.GetDataTable(sql1);
        if (dt1.Rows.Count > 0)
            return false;
        else return true;

    }
    protected bool Check_Email(string email)
    {
        string sql2 = string.Format(@"select Mail From customers Where Mail='{0}'", email);
        DataTable dt2 = DataBase.GetDataTable(sql2);
        if (dt2.Rows.Count > 0)
            return false;
        else return true;
    }
   
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }

    protected void Check_Click(object sender, EventArgs e)
    {
        Check_Input();
        
    }
    protected void Register_Click(object sender, EventArgs e)
    {
        Check_Input();
        if (inputOK)
        {
            try
            {
                string sql = string.Format(@"Insert into customers (CustomerID,Password,FullName,Mail,SDT,DiaChi) Values('{0}','{1}','{2}','{3}','{4}','{5}')", txtName.Text,
                    txtPass1.Text, txtFName.Text, txtEmail.Text, txtPhone.Text, txtDiaChi.Text);
                DataBase.ExecuteNonQuery(sql);
                Response.Redirect("~/ThankYou.aspx");
            }
            catch
            {
                test.Text = "Xin bạn vui lòng nhập lại thông tin";
            }
        }

    }
}
